/**
 * 
 */
package org.meruvian.yama.demo.service;

import java.util.Date;

import org.meruvian.yama.demo.dao.PersonDao;
import org.meruvian.yama.demo.entity.Person;
import org.meruvian.yama.persistence.EntityListWrapper;
import org.meruvian.yama.persistence.LogInformation.StatusFlag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Dian Aditya
 * 
 */
@Service
@Transactional
public class PersonServiceImpl implements PersonService {

	@Autowired
	private PersonDao personDao;

	@Override
	@Transactional(readOnly = true)
	public EntityListWrapper<Person> find(Person person, int max, int page) {
		return personDao.getByExample(person, max, page);
	}

	@Override
	public void save(Person person) {
		if (person == null)
			return;

		person.setId(null);
		personDao.persist(person);
	}

	@Override
	public void update(Person person) {
		if (person == null)
			return;
		if (person.getId() == null)
			return;

		person.getLogInformation().setUpdateDate(new Date());

		personDao.merge(person);
	}

	@Override
	public void delete(Person person) {
		person.getLogInformation().setStatusFlag(StatusFlag.INACTIVE);

		update(person);
	}

	@Override
	public Person findSingle(String id) {
		return personDao.findById(id);
	}

}
